<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>

<%@ taglib prefix="pu" uri="/WEB-INF/tld/pageutil.tld"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<%@ include file="/taglibs.jsp"%>

<c-rt:set var="midContextPath" value="<%=ServerValue.getMidContextPath()%>" />
<c-rt:set var="imgDomain" value="<%=ServerValue.getImgDomain()%>" />

<script src="${midContextPath}/js/jquery.form.js" type="text/javascript" charset="UTF-8"></script>

<script language="javascript">
	var loadingDialog=$("#loadingImg");//不在這一頁
	var productSelectWindow=$("#productSelectWindow");//不在這一頁
	
	$(function(){
		var prdSearchForm=$("#prdSearchForm");
		
		//查詢
		$("#searchPrdBtn").click(function(){
			prdSearchForm.ajaxSubmit({
				url:'${midContextPath}/productRC/productSearch.do',
				cache:false,
				type:'post',
				beforeSend:function(){
					loadingDialog.dialog('open');
				},
				success:function(strData){
					loadingDialog.dialog('close');
					productSelectWindow.html(strData);
				}
			});
		});
		
		//加入商品
		$("#addProdBtn").click(function(){
			var prdRCCount=$("input[id*=id_]").size();
			var addPrdCount=$("input[id*=addProdCb_]:checked").size();
			if(addPrdCount==0){
				alert("请选择欲加入的商品");
			}else if((prdRCCount+addPrdCount) > 10){
				alert("馆长推荐商品超过10笔");
			}else if(!checkPrd()){
				alert("同分类下此笔商品已经被加入馆长推荐");
			}else{
				productSelectWindow.dialog('close');
				addToProductRC();
			}
		});
	});
	
	function checkPrd(){
		var prdRCCount=$("input[id*=id_]").size();
		var isOK=true;
		
		$("input[id*=addProdCb_]:checked").each(function(){
			var addCategoryPath=$("#addCategoryPath_"+this.value).attr("value");
			var addProductId=$("#addProductId_"+this.value).attr("value");
			var addProdCbObj=this;
			
			$("input[id*=rowsIndex_]").each(function(){
				var categoryPath=$("#categoryPath_"+this.value).attr("value");
				var productId=$("#productId_"+this.value).attr("value");
				if(addCategoryPath==categoryPath && addProductId==productId){
					addProdCbObj.focus();
					isOK=false;
					return false;
				}
			});
			
			if(!isOK) return false;
		});
		
		return isOK;
	}
	
	function addToProductRC(){
		//$("#loadingImg").dialog('open');
		
		var imgDomain="${imgDomain}";
		
		$("input[id*=addProdCb_]:checked").each(function(){
			var prdRCCount;
			for(var i=0;i<10;i++){
				if($("input[id*=id_"+i+"]").size()==0){
					prdRCCount=i;
					break;
				}
			}
			
			var addPrdCateId="${categoryId}";
			var addCategoryPath=$("#addCategoryPath_"+this.value).attr("value");
			var substrProdId=$("#addProductId_"+this.value).attr("value").substring(0,4);
			var addProductId=$("#addProductId_"+this.value).attr("value");
			var addProductName=$("#addProductName_"+this.value).attr("value");
			var addPromoteName=$("#addPromoteName_"+this.value).attr("value");
			var addShelfPrice=$("#addShelfPrice_"+this.value).attr("value");
			var addListPrice=$("#addListPrice_"+this.value).attr("value");
			
			var htmlStr=
				'<tr>'+
					'<td>'+
						'<input type="checkbox" id="cb_'+prdRCCount+'" name="delPrdRCIds" value="" />'+
						'<input type="hidden" name="id_'+prdRCCount+'" id="id_'+prdRCCount+'" value="" />'+
						'<input type="hidden" name="prdCateId_'+prdRCCount+'" value="'+addPrdCateId+'" />'+
						'<input type="hidden" name="categoryPath_'+prdRCCount+'" id="categoryPath_'+prdRCCount+'" value="'+addCategoryPath+'" />'+
						'<input type="hidden" name="rowsIndex_'+prdRCCount+'" id="rowsIndex_'+prdRCCount+'" value="'+prdRCCount+'" />'+
						'<input type="hidden" name="promoteName_'+prdRCCount+'" value="'+addPromoteName+'" />'+
						'<input type="hidden" name="shelfPrice_'+prdRCCount+'" value="'+addShelfPrice+'" />'+
						'<input type="hidden" name="listPrice_'+prdRCCount+'" value="'+addListPrice+'" />'+
					'</td>'+
					'<td>'+
						'<input type="text" id="sortWeight_'+prdRCCount+'" name="sortWeight_'+prdRCCount+'" value="500" maxlength="5" size="5" onkeyup="value=value.replace(/[^\\d]/g,&#39;&#39;)" onbeforepaste="clipboardData.setData(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^\\d]/g,&#39;&#39;))" />'+
					'</td>'+
					'<td id="sortOrder_'+prdRCCount+'"></td>'+
					'<td><img src="'+imgDomain+'/PImg/'+substrProdId+'/'+addProductId+'/'+addProductId+'_m75.jpg" onError="this.src=\'/PImg/blank_75.gif\'" width="75" height="75" /></td>'+
					'<td><input name="advTitle_'+prdRCCount+'" id="advTitle_'+prdRCCount+'" type="text" size="4" value="" maxlength="4" /><br/><input type="radio" name="advStyle_'+prdRCCount+'" value="0" checked="checked" />红<input type="radio" name="advStyle_'+prdRCCount+'" value="1" />绿</td>'+
					'<td>'+
						addProductId+
						'<input type="hidden" name="productId_'+prdRCCount+'" id="productId_'+prdRCCount+'" value="'+addProductId+'" />'+
					'</td>'+
					'<td>'+
						addProductName+
						'<input type="hidden" name="productName_'+prdRCCount+'" value="'+addProductName+'" />'+
					'</td>'+
				'</tr>';
				
			$("#sortTbody").append(htmlStr);
		});
		/*
		$("#productForm").ajaxSubmit({
			url:'${midContextPath}/productRC/ajaxDraft.do',
			cache:false,
			type:'post',
			success:function(strData){
				$("#loadingImg").dialog('close');
			}
		});
		*/
		$("#productForm").attr("action","${midContextPath}/productRC/draft.do");
		$("#productForm").submit();
	}
</script>

<form id="prdSearchForm">
	<select>
		<option>${categoryName}</option>
	</select>
	关键字：<input type="text" name="keyword" value="${keyword}" /><input type="button" id="searchPrdBtn" name="searchPrdBtn" value="检索" />
</form>

<form id="prdForm">
	<table class="tb-1">
		<thead>
			<tr>
				<th></th>
				<th>商品图</th>
				<th>商品编号</th>
				<th>商品名称</th>
			</tr>
		</thead>
		
		<tbody>
		<c-rt:forEach items="${productList}" var="product" varStatus="rows_index">
			<tr>
				<td><input type="checkbox" id="addProdCb_${rows_index.index}" value="${rows_index.index}" /></td>
				<td><img src="${imgDomain}/PImg/${pu:substring(product.product_id,0,4)}/${product.product_id}/${product.product_id}_m75.jpg" onError="this.src='/PImg/blank_75.gif'" width="75" height="75" /></td>
				<td>${product.product_id}</td>
				<td>${product.product_name}</td>
			</tr>
			<input type="hidden" id="addCategoryPath_${rows_index.index}" value="${product.category_path}" />
			<input type="hidden" id="addProductId_${rows_index.index}" value="${product.product_id}" />
			<input type="hidden" id="addProductName_${rows_index.index}" value="${product.product_name}" />
			<input type="hidden" id="addPromoteName_${rows_index.index}" value="${product.promote_name}" />
			<input type="hidden" id="addShelfPrice_${rows_index.index}" value="${product.shelf_price}" />
			<input type="hidden" id="addListPrice_${rows_index.index}" value="${product.list_price}" />
		</c-rt:forEach>
		</tbody>
		
		<tbody>
			<tr>
				<td colspan="4" align="left">
					<input type="button" name="addProdBtn" id="addProdBtn" value="加入" />
				</td>
			</tr>
		</tbody>
	</table>
	<yaodian100:pageutil link="list.do" pagerObj="${pager}" />
</form>

<script language="javascript">
	$(function() {
		var pageLinks = $("div.page-change a");
		//分頁動作調整
		pageLinks.click(function(e){
			e.preventDefault();//取消連結原有的功能
			
			var href = $(this).attr("href");
			var pageNumber = extractPageNumberFromHref(href);
			
			$.ajax({
				url: '${midContextPath}/productRC/productSearch.do',
				type: 'post',
				data: {'pager.currentPage':pageNumber},
				cache: false,
				beforeSend: function(){
					loadingDialog.dialog('open');
				},
				success: function(strData){
					loadingDialog.dialog('close');
					productSelectWindow.html(strData);
					$('html, body').animate( { scrollTop: 0 }, 0 );
				}
			});
			return false;
		});
	});
	
	//從分頁連結抓出要連結的目標頁碼
	function extractPageNumberFromHref(href) {
		var splitByQuestionMark = href.split('?');
		if (splitByQuestionMark.length < 2) {
			return '';
		}
		var parameterArray = splitByQuestionMark[1].split('&');
		for (var i = 0; i < parameterArray.length; i++) {
			var parameterPair = parameterArray[i].split('=');
			if (parameterPair[0] == 'pager.currentPage') {
				return parameterPair[1];
			}
		}
		return '';
	}
</script>